package com.lz.football.module.playerCenter.ModificationNumber
{
	import com.common.utils.Reflection;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.PlayerCenterEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.playerCenter.vo.PlayerVo;
	import com.lz.football.module.club.item.BaseItem;
	
	import common.BaseView.BaseView;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.nickScroll.HScrollBarWithBar;

	/**
	 * 修改球员号码 
	 * @author Administrator
	 * 
	 */	
	public class ModificationNumber extends BaseView
	{
		/**
		 * 
		 */
		private var listContain:MovieClip;
		/**
		 * 
		 */
		private var _bar:HScrollBarWithBar;
		/**
		 * 
		 */
		private var _target:Sprite;
		/**
		 * 
		 */
		private var _selectItem:BaseItem;
		/**
		 *输入文本 
		 */
		private var inputText:TextField;
		/**
		 *保存 
		 */
		private var savebtn:SimpleButton;
		/**
		 *文本提示 
		 */
		private var tips:TextField;
		private var _playerList:Array;
		public function ModificationNumber()
		{
			super();
		}
		override public function initUI(view:Object = null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.modul.playerCenter.modificationNumber");
			_windowBg = symbolSkin.windowBg;
			_viewWidth = _windowBg.width;
			_viewHeight = _windowBg.height;
			inputText = symbolSkin.inputText;
			inputText.maxChars = 2;
			inputText.restrict = "0-9";
			inputText.text = "";
			savebtn = symbolSkin.savebtn;
			tips = symbolSkin.tips;
			tips.visible = false;
			this.addChild(symbolSkin);
			listContain = symbolSkin.listContain;
			isDrag = true;
			isCloseBtn = true;
			super.initUI();
			moveClose(_windowBg.width - 35,0);
			initBar();
			addEvtListener();
		}
		/**
		 * 
		 * 
		 */
		private function initBar():void
		{
			_bar = new HScrollBarWithBar();
			_target = new Sprite();
			_bar.setSize(580,100);
			listContain.addChild(_bar);
			_bar.target = _target;
		}
		/**
		 *更新 
		 * 
		 */
		public function upDataList():void
		{
			resetTarget();
			var item:ModificationNumberItem;
			_playerList = AppData.getInstance().playerCenterDataManager.playerList;
			_playerList.sortOn("number",Array.NUMERIC);
			var len:int = _playerList.length;
			for(var i:int = 0;i < len;i++)
			{
				item = new ModificationNumberItem();
				item.initUI();
				_target.addChild(item);
				item.x = 58 * i;
				item.playerVo = _playerList[i];
				item.callBack = selectItem;
			}
			_bar.targetHeight = 58 *_target.numChildren;
		}
		/**
		 *重置 
		 * 
		 */
		private function resetTarget():void
		{
			var item:ModificationNumberItem;
			while(_target.numChildren)
			{
				item = _target.removeChildAt(0) as ModificationNumberItem;
				item.destroyView();
				item = null;
			}
			_target.x = 0;
		}
		/**
		 *选择了item; 
		 * @param $item
		 * 
		 */
		private function selectItem($item:BaseItem):void
		{
			if(_selectItem != null)_selectItem.select = false;
			_selectItem = $item;
			_selectItem.select = true;
		}
		override public function addEvtListener():void
		{
			super.addEvtListener();
			savebtn.addEventListener(MouseEvent.CLICK,onSaveHandler);
//			inputText.addEventListener(TextEvent.TEXT_INPUT,onInputHandler);
			inputText.addEventListener(Event.CHANGE,onInputHandler);
		}
		
		/**
		 *输入 文本 
		 * @param event
		 * 
		 */
		protected function onInputHandler(event:Event):void
		{
			var len:int = _playerList.length;
			for(var i:int = 0;i < len;i++)
			{
				if(_playerList[i].number == int(inputText.text))
				{
					tips.visible = true;
					return;
				}
			}
			tips.visible = false;
		}
		
		protected function onSaveHandler(event:MouseEvent):void
		{
			if(_selectItem == null)
			{
				Alert.showErrorTip("请选择要修改的球员");
				return;
			}
			if(inputText.text == "")
			{
				Alert.showErrorTip("请输入号码");
				return;
			}
			var number:int = int(inputText.text);
			var len:int = _playerList.length;
			for(var i:int = 0;i < len;i++)
			{
				if(_playerList[i].number == number)
				{
					Alert.showErrorTip("已经有相同的球员号码");
					return;
				}
			}
			savebtn.removeEventListener(MouseEvent.CLICK,onSaveHandler);
			AppEventDispatcher.getInstance().dispatchEvent(new PlayerCenterEvent(PlayerCenterEvent.CHANGE_PLAYER_NUMBER,
				{arr:[{playerId:(_selectItem.data as PlayerVo).playerid,number:number}]}));
		}
		/**
		 *球员号码修改 
		 * @param $opj
		 * 
		 */
		public function changePlayerNumber($obj:Object):void
		{
			savebtn.addEventListener(MouseEvent.CLICK,onSaveHandler);
			if(!$obj.status)return;
			var len:int = _target.numChildren;
			var item:ModificationNumberItem;
			while(len)
			{
				item = _target.getChildAt(len-1) as ModificationNumberItem;
				if(item.playerVo.playerid == $obj.playerId)
				{
					item.upDataNumber();
					break;
				}
				len--;
			}
		}
		override public function removeEvtListener():void
		{
			super.removeEvtListener();
			savebtn.removeEventListener(MouseEvent.CLICK,onSaveHandler);
			//			inputText.addEventListener(TextEvent.TEXT_INPUT,onInputHandler);
			inputText.removeEventListener(Event.CHANGE,onInputHandler);
		}
		override public function destroyView():void
		{
			resetTarget();
			removeEvtListener();
			super.destroyView();
		}
	}
}